home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / BasicLabelUI.java < prev    next >
Text File  |  1998-06-30  |  10KB  |  325 lines

  1. /*
  2.  * @(#)BasicLabelUI.java    1.48 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.basic;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.plaf.*;
  25.  
  26. import java.awt.event.ActionEvent;
  27. import java.awt.Component;
  28. import java.awt.Container;
  29. import java.awt.Dimension;
  30. import java.awt.Rectangle;
  31. import java.awt.Insets;
  32. import java.awt.Color;
  33. import java.awt.Graphics;
  34. import java.awt.Font;
  35. import java.awt.FontMetrics;
  36. import java.io.Serializable;
  37. import java.beans.PropertyChangeEvent;
  38. import java.beans.PropertyChangeListener;
  39. import com.sun.java.swing.AbstractAction;
  40.  
  41. /**
  42.  * A Windows L&F implementation of LabelUI.  This implementation 
  43.  * is completely static, i.e. there's only one UIView implementation 
  44.  * that's shared by all JLabel objects.
  45.  * <p>
  46.  * Warning: serialized objects of this class will not be compatible with
  47.  * future swing releases.  The current serialization support is appropriate
  48.  * for short term storage or RMI between Swing1.0 applications.  It will
  49.  * not be possible to load serialized Swing1.0 objects with future releases
  50.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  51.  * baseline for the serialized form of Swing objects.
  52.  *
  53.  * @version 1.48 02/02/98
  54.  * @author Hans Muller
  55.  */
  56. public class BasicLabelUI extends LabelUI 
  57.     implements Serializable, PropertyChangeListener
  58. {
  59.     protected static BasicLabelUI labelUI = new BasicLabelUI();
  60.  
  61.     /**
  62.      * Forwards the call to SwingUtilities.layoutCompoundLabel().
  63.      * This method is here so that a subclass could do Label specific
  64.      * layout and to shorten the method name a little.
  65.      * 
  66.      * @see SwingUtilities#layoutCompoundLabel
  67.      */
  68.     protected String layoutCL(
  69.         JLabel label,               
  70.         FontMetrics fontMetrics, 
  71.     String text, 
  72.     Icon icon, 
  73.     Rectangle viewR, 
  74.     Rectangle iconR, 
  75.     Rectangle textR)
  76.     {
  77.         return SwingUtilities.layoutCompoundLabel(
  78.             fontMetrics,
  79.         text,
  80.         icon,
  81.         label.getVerticalAlignment(),
  82.         label.getHorizontalAlignment(),
  83.         label.getVerticalTextPosition(),
  84.         label.getHorizontalTextPosition(),
  85.         viewR,
  86.         iconR,
  87.         textR,
  88.         label.getIconTextGap());
  89.     }
  90.  
  91.     /**
  92.      * Paint clippedText at textX, textY with the labels foreground color.
  93.      * 
  94.      * @see #paint
  95.      * @see #paintDisabledText
  96.      */
  97.     protected void paintEnabledText(JLabel l, Graphics g, String s, int textX, int textY)
  98.     {
  99.     int accChar = l.getDisplayedMnemonic();
  100.     g.setColor(l.getForeground());
  101.     BasicGraphicsUtils.drawString(g, s, accChar, textX, textY);
  102.     }
  103.  
  104.  
  105.     /**
  106.      * Paint clippedText at textX, textY with background.lighter() and then 
  107.      * shifted down and to the right by one pixel with background.darker().
  108.      * 
  109.      * @see #paint
  110.      * @see #paintEnabledText
  111.      */
  112.     protected void paintDisabledText(JLabel l, Graphics g, String s, int textX, int textY)
  113.     {
  114.     int accChar = l.getDisplayedMnemonic();
  115.     Color background = l.getBackground();
  116.     g.setColor(background.brighter());
  117.     BasicGraphicsUtils.drawString(g, s, accChar, textX, textY);
  118.     g.setColor(background.darker());
  119.     BasicGraphicsUtils.drawString(g, s, accChar, textX + 1, textY + 1);
  120.     }
  121.  
  122.  
  123.     /** 
  124.      * Paint the label text in the foreground color, if the label
  125.      * is opaque then paint the entire background with the background
  126.      * color.  The Label text is drawn by paintEnabledText() or
  127.      * paintDisabledText().  The locations of the label parts are computed
  128.      * by layoutCL.
  129.      * 
  130.      * @see #paintEnabledText
  131.      * @see #paintDisabledText
  132.      * @see #layoutCL
  133.      */
  134.     public void paint(Graphics g, JComponent c) 
  135.     {
  136.     JLabel label = (JLabel)c;
  137.     String text = label.getText();
  138.     Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();
  139.  
  140.     if ((icon == null) && (text == null)) {
  141.         return;
  142.     }
  143.  
  144.     FontMetrics fm = g.getFontMetrics();
  145.     Rectangle iconR = new Rectangle();
  146.     Rectangle textR = new Rectangle();
  147.     Rectangle viewR = new Rectangle(c.getSize());
  148.     Insets viewInsets = c.getInsets();
  149.  
  150.     viewR.x = viewInsets.left;
  151.     viewR.y = viewInsets.top;
  152.     viewR.width -= (viewInsets.left + viewInsets.right);
  153.     viewR.height -= (viewInsets.top + viewInsets.bottom);
  154.  
  155.     String clippedText = layoutCL(label, fm, text, icon, viewR, iconR, textR);
  156.  
  157.     if (icon != null) {
  158.         icon.paintIcon(c, g, iconR.x, iconR.y);
  159.     }
  160.  
  161.     if (text != null) {
  162.         int textX = textR.x;
  163.         int textY = textR.y + fm.getAscent();
  164.  
  165.         if (label.isEnabled()) {
  166.         paintEnabledText(label, g, clippedText, textX, textY);
  167.         }
  168.         else {
  169.         paintDisabledText(label, g, clippedText, textX, textY);
  170.         }
  171.     }
  172.     }
  173.  
  174.  
  175.     public Dimension getPreferredSize(JComponent c) 
  176.     {
  177.     JLabel label = (JLabel)c;
  178.     String text = label.getText();
  179.     Icon icon = label.getIcon();
  180.     Insets insets = label.getInsets();
  181.     Font font = label.getFont();
  182.  
  183.     int dx = insets.left + insets.right;
  184.     int dy = insets.top + insets.bottom;
  185.  
  186.     if ((icon == null) && ((text == null) || ((text != null) && (font == null)))) {
  187.         return new Dimension(dx, dy);
  188.     }
  189.     else if ((text == null) || ((icon != null) && (font == null))) {
  190.         return new Dimension(icon.getIconWidth() + dx, icon.getIconHeight() + dy);
  191.     }
  192.     else {
  193.         FontMetrics fm = label.getToolkit().getFontMetrics(font);
  194.  
  195.         Rectangle iconR = new Rectangle();
  196.         Rectangle textR = new Rectangle();
  197.         Rectangle viewR = new Rectangle(dx, dy, Short.MAX_VALUE, Short.MAX_VALUE);
  198.         layoutCL(label, fm, text, icon, viewR, iconR, textR);
  199.         Dimension rv = iconR.union(textR).getSize();
  200.         rv.width += dx;
  201.         rv.height += dy;
  202.         return rv;
  203.     }
  204.     }
  205.  
  206.  
  207.     /**
  208.      * @return getPreferredSize(c)
  209.      */
  210.     public Dimension getMinimumSize(JComponent c) {
  211.     return getPreferredSize(c);
  212.     }
  213.  
  214.     /**
  215.      * @return getPreferredSize(c)
  216.      */
  217.     public Dimension getMaximumSize(JComponent c) {
  218.     return getPreferredSize(c);
  219.     }
  220.  
  221.  
  222.     public void installUI(JComponent c) { 
  223.         LookAndFeel.installColorsAndFont(c, "Label.background", "Label.foreground", "Label.font");
  224.         installKeyboardActions(c);        // for labelFor/accel binding
  225.         c.addPropertyChangeListener(this);    // "   "              "
  226.     }
  227.  
  228.     public void uninstallUI(JComponent c) { 
  229.         uninstallKeyboardActions(c);
  230.         c.removePropertyChangeListener(this);
  231.     }
  232.     
  233.     protected void installKeyboardActions(JComponent c) {
  234.     JLabel l = (JLabel) c;
  235.         int dka = l.getDisplayedMnemonic();
  236.     Component lf = l.getLabelFor();
  237.         c.resetKeyboardActions();
  238.     if ((dka != 0) && (lf != null)) {
  239.         l.registerKeyboardAction(
  240.             new PressAction(l,lf),
  241.             KeyStroke.getKeyStroke(dka,ActionEvent.ALT_MASK,false),
  242.             JComponent.WHEN_IN_FOCUSED_WINDOW);
  243.     }
  244.     }
  245.  
  246.     protected void uninstallKeyboardActions(JComponent c) {
  247.         c.resetKeyboardActions();
  248.     }
  249.  
  250.     public static ComponentUI createUI(JComponent c) {
  251.     return labelUI;
  252.     }
  253.  
  254.     public void propertyChange(PropertyChangeEvent e) {
  255.     if (e.getPropertyName().equals("labelFor") ||
  256.         e.getPropertyName().equals("displayedMnemonic")) {
  257.         installKeyboardActions((JLabel) e.getSource());
  258.     }
  259.     }
  260.  
  261.     // When the accelerator is pressed, temporarily make the JLabel 
  262.     // focusTraversable by registering a WHEN_FOCUSED action for the
  263.     // release of the accelerator.  Then give it focus so it can 
  264.     // prevent unwanted keyTyped events from getting to other components.
  265.     static class PressAction extends AbstractAction {
  266.     JLabel      owner;
  267.         Component labelFor;
  268.  
  269.         PressAction(JLabel l, Component c) {
  270.         super("nothing");
  271.         owner = l;
  272.         labelFor = c;
  273.     }
  274.  
  275.     public void actionPerformed(ActionEvent e) {
  276.         owner.registerKeyboardAction(
  277.             new ReleaseAction(owner,labelFor),
  278.             KeyStroke.getKeyStroke(owner.getDisplayedMnemonic(),
  279.                                            ActionEvent.ALT_MASK,true),
  280.             JComponent.WHEN_FOCUSED);
  281.  
  282.         // Need this if the accelerator is released before the ALT key
  283.         //
  284.         owner.registerKeyboardAction(
  285.             new ReleaseAction(owner,labelFor),
  286.             KeyStroke.getKeyStroke(0,ActionEvent.ALT_MASK,true),
  287.             JComponent.WHEN_FOCUSED);
  288.  
  289.         owner.requestFocus();
  290.         }
  291.  
  292.     public boolean isEnabled() {
  293.         return owner.isEnabled();
  294.     }
  295.     }
  296.  
  297.     // On the release of the accelerator, remove the keyboard action
  298.     // that allows the label to take focus and then give focus to the
  299.     // labelFor component.
  300.     static class ReleaseAction extends AbstractAction {
  301.     JLabel      owner;
  302.         Component labelFor;
  303.  
  304.         ReleaseAction(JLabel l, Component c) {
  305.         super("giveFocusToLabelFor");
  306.         owner = l;
  307.         labelFor = c;
  308.     }
  309.     
  310.     public void actionPerformed(ActionEvent e) {
  311.         owner.unregisterKeyboardAction(
  312.             KeyStroke.getKeyStroke(owner.getDisplayedMnemonic(),
  313.                        ActionEvent.ALT_MASK,true));
  314.         owner.unregisterKeyboardAction(
  315.             KeyStroke.getKeyStroke(0,ActionEvent.ALT_MASK,true));
  316.         labelFor.requestFocus();
  317.         }
  318.  
  319.     public boolean isEnabled() {
  320.         return owner.isEnabled();
  321.     }
  322.     }
  323. }
  324.  
  325.